﻿Imports System.Data.SqlClient

Public Class TRXLogger

    Public Shared Sub Log(ByVal _testRun As TestRun)

        'If Not TRXLogger.TestRunExiste(_testRun) Then
        '    TRXLogger.SalvarTestRun(_testRun)
        'End If

    End Sub

    Public Shared Function ConvertData(ByVal _convertData As String) As String

        Dim _data As String
        _data = _convertData.Replace("T", " ")
        _data = Split(_data, ".").First
        Return _data

    End Function


    Public Shared Function ClassNameByMethod(ByVal _testRun As TestRun, ByVal _methodName As String) As String

        Dim q = From c In _testRun.TestDefinitions.UnitTest Where c.TestMethod.name = _methodName Select c

        If q.Count > 0 Then
            Return q.First.TestMethod.className
        Else
            Return ""
        End If

    End Function

    Public Shared Function TestRunExiste(ByVal _testRun As TestRun) As Boolean

        'Dim _connection As IDbConnection = Nothing
        'Dim _command As IDbCommand
        'Dim _dataReader As IDataReader
        'Dim _existe As Boolean
        'Dim _sql As String

        'Try
        '    '-- fabrica
        '    _connection = New SqlConnection("Data Source=localhost;Initial Catalog=bd;Persist Security Info=True;User ID=W_TESTE;Password=W_TESTE;")
        '    '-- fabrica

        '    _connection.Open()

        '    _sql = " Select 1 From [UnitTestResult] " & _
        '              " Where [name] = '" & _testRun.name & "'"

        '    _command = New SqlCommand(_sql, _connection)

        '    _dataReader = _command.ExecuteReader()

        '    _existe = _dataReader.Read()

        'Catch ex As Exception
        '    Console.WriteLine(ex.Message)
        '    _existe = False
        'Finally
        '    If _connection IsNot Nothing Then _connection.Close()
        '    _connection.Dispose()
        'End Try

        'Return _existe

    End Function

    Public Shared Sub SalvarTestRun(ByVal _testRun As TestRun)

        'Dim _connection As IDbConnection = Nothing
        'Dim _command As IDbCommand
        'Dim _sql As String
        ''_sql = " insert into TestRun ([Result], [TestName], [ProjectName], [Duration], [ClassName], [TimeStamp]) " & _
        ''       " values (@Result, @TestName, @ProjectName, @Duration, @ClassName, @TimeStamp)"

        'Try
        '    '-- fabrica
        '    _connection = New SqlConnection("Data Source=localhost;Initial Catalog=bd;Persist Security Info=True;User ID=W_TESTE;Password=W_TESTE;")
        '    '-- fabrica

        '    _connection.Open()

        '    For Each _item In _testRun.TestRunConfiguration.Deployment.DeploymentItems

        '        _sql = " INSERT INTO DeploymentItems " & _
        '                " ([IdTest] ,[RunUser] ,[name] ,[fileName]) " & _
        '                " VALUES (@IdTest , @RunUser ,@name , @fileName ) "

        '        _command = New SqlCommand(_sql, _connection)


        '        _command.Parameters.Add(New SqlParameter("@IdTest", _testRun.idTest))
        '        _command.Parameters.Add(New SqlParameter("@RunUser", _testRun.runUser))
        '        _command.Parameters.Add(New SqlParameter("@name", _testRun.name))
        '        _command.Parameters.Add(New SqlParameter("@fileName", _item.fileName))

        '        _command.ExecuteNonQuery()

        '        _command.Parameters.Clear()

        '    Next


        '    For Each _unitTest In _testRun.Results.UnitTestResult

        '        _sql = " INSERT INTO [UnitTestResult] " & _
        '       " ([IdTest],[computerName],[runUser],[name] " & _
        '       " ,[duration],[testName],[outcome], [className] " & _
        '       " ,[executionId],[startTime],[endTime], [runDeploymentRoot] , [assembly]) " & _
        '       " VALUES  " & _
        '       " ( @IdTest , @computerName , " & _
        '       " @runUser, @name , " & _
        '       " @duration, @testName, " & _
        '       " @outcome , @className, @executionId , " & _
        '       " @startTime , @endTime, @runDeploymentRoot, @assembly ) "

        '        _command = New SqlCommand(_sql, _connection)

        '        Dim _fullyQualified As String = TRXLogger.ClassNameByMethod(_testRun, _unitTest.testName)
        '        Dim _assembly As String = _fullyQualified.Split(",").Last.Trim
        '        Dim _classe As String = _fullyQualified.Split(",").First.Trim

        '        _command.Parameters.Add(New SqlParameter("@IdTest", _testRun.idTest))
        '        _command.Parameters.Add(New SqlParameter("@runUser", _testRun.runUser))
        '        _command.Parameters.Add(New SqlParameter("@name", _testRun.name))
        '        _command.Parameters.Add(New SqlParameter("@duration", _unitTest.duration))
        '        _command.Parameters.Add(New SqlParameter("@testName", _unitTest.testName))
        '        _command.Parameters.Add(New SqlParameter("@className", _classe))
        '        _command.Parameters.Add(New SqlParameter("@outcome", _unitTest.outcome))
        '        _command.Parameters.Add(New SqlParameter("@computerName", _unitTest.computerName))
        '        _command.Parameters.Add(New SqlParameter("@executionId", _unitTest.executionId))
        '        _command.Parameters.Add(New SqlParameter("@startTime", TRXLogger.ConvertData(_unitTest.startTime)))
        '        _command.Parameters.Add(New SqlParameter("@endTime", TRXLogger.ConvertData(_unitTest.endTime)))
        '        _command.Parameters.Add(New SqlParameter("@runDeploymentRoot", _testRun.TestRunConfiguration.Deployment.runDeploymentRoot))
        '        _command.Parameters.Add(New SqlParameter("@assembly", _assembly))

        '        _command.ExecuteNonQuery()

        '        _command.Parameters.Clear()
        '    Next
        'Catch ex As Exception
        '    Console.WriteLine(ex.Message)
        'Finally
        '    If _connection IsNot Nothing Then _connection.Close()
        '    _connection.Dispose()
        'End Try
        

        '_sql = " insert into TestRun ([Result], [TestName], [ProjectName], [Duration], [ClassName], [TimeStamp]) " & _
        '        " values (@Result, @TestName, @ProjectName, @Duration, @ClassName, @TimeStamp)"

        '_sql = "insert into TestReport ( [IdReport] , [Start] , [total] , [executed] , " & _
        '        "       [passed] , [failed] , [timeout] , [inconclusive] , [arquivo] ) " & _
        '        "   values " & _
        '        "   (   @IdReport , @Start , @total , @executed , " & _
        '        "       @passed , @failed , @timeout , @inconclusive , @arquivo ) "

        '_command.Parameters.Add(New SqlParameter("@IdTest", _testRun.idTest))
        '_command.Parameters.Add(New SqlParameter("@runUser", _testRun.runUser))
        '_command.Parameters.Add(New SqlParameter("@name", _testRun.name))
        '_command.Parameters.Add(New SqlParameter("@runDeploymentRoot", _testRun.TestRunConfiguration.Deployment.runDeploymentRoot))
        '_command.Parameters.Add(New SqlParameter("@aborted", _testRun.ResultSummary.Counters.aborted))
        '_command.Parameters.Add(New SqlParameter("@completed", _testRun.ResultSummary.Counters.completed))
        '_command.Parameters.Add(New SqlParameter("@passed", _testRun.ResultSummary.Counters.passed))
        '_command.Parameters.Add(New SqlParameter("@failed", _testRun.ResultSummary.Counters.failed))
        '_command.Parameters.Add(New SqlParameter("@inconclusive", _testRun.ResultSummary.Counters.inconclusive))
        '_command.Parameters.Add(New SqlParameter("@timeout", _testRun.ResultSummary.Counters.timeout))
        '_command.Parameters.Add(New SqlParameter("@total", _testRun.ResultSummary.Counters.total))
        '_command.Parameters.Add(New SqlParameter("@arquivo", "-"))

        '_command.ExecuteNonQuery()

    End Sub

    Public Shared Sub SalvarTestReport(ByVal _testRun As TestRun)

    End Sub

End Class
